package Formularios;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Toolkit;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;



public class frmLogin extends JFrame {

	private JPanel contentPane;
	private JTextField txtUsuario;
	private JPasswordField txtContraseña;
	
	private int iEmpleado = cNemonicos._ERROR_;
	private String sNombreEmpleado;
	
	static Statement comando;
	static Connection con;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frmLogin frame = new frmLogin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public frmLogin() {
		setTitle("Login");
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(frmLogin.class.getResource("/com/sun/java/swing/plaf/windows/icons/Computer.gif")));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 223, 257);
		//Centrar
		setLocationRelativeTo(null);
		
		final general gen = new general();
		try {
			comando = gen.abrirConexion(cNemonicos.IP_GLOBAL,1);
		} catch (ClassNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblUsuario = new JLabel("USUARIO:");
		
		txtUsuario = new JTextField();
		txtUsuario.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if ( arg0.getKeyCode() == cNemonicos.VK_SALTO_DE_LINEA)
				{
					txtContraseña.requestFocus();
				}
			}
		});
		
		txtUsuario.setColumns(10);
		
		JLabel lblContrasea = new JLabel("CONTRASEÑA :");
		
		txtContraseña = new JPasswordField();
		txtContraseña.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if ( e.getKeyCode() == cNemonicos.VK_SALTO_DE_LINEA )
				{
					String sUsuario = txtUsuario.getText();
					String sContraseña = txtContraseña.getText();
					int iNumEmpleado = general.validarUsuario(comando, sUsuario, sContraseña);
					if ( iNumEmpleado != cNemonicos._ERROR_ )
					{
						iEmpleado = iNumEmpleado;
						sNombreEmpleado = general.consultarNombreEmpleado(comando, iEmpleado);
						dispose();
					}
					else
					{
						iEmpleado = cNemonicos._ERROR_;
						JOptionPane.showOptionDialog (null,"Usuario o Contraseña Invalidos","No Existe Usuario",JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE,null,new Object[] {"ACEPTAR"},"ACEPTAR");
					}
				}
			}
		});
		txtContraseña.setColumns(10);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				 try {
					 
					 Map parameters = new HashMap();
				      parameters.put("TITULO", "PAISES");
				      parameters.put("FECHA", new java.util.Date());
				      
				      
					JasperReport report = JasperCompileManager.compileReport("/run/media/luis/Respaldo/Proyectos/proyectosLinuxOnline/proyectos/Rohen_Clean/Reportes/FinDia.jrxml");
					JasperPrint print = JasperFillManager.fillReport(report, parameters, gen.con);
					
					 JasperExportManager.exportReportToPdfFile(print,
					          "/run/media/luis/Respaldo/Proyectos/proyectosLinuxOnline/proyectos/Rohen_Clean/Reportes/prueba.pdf");
					      //Para visualizar el pdf directamente desde java
					      JasperViewer.viewReport(print, false);
				} catch (JRException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(txtUsuario, GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
						.addComponent(lblUsuario)
						.addComponent(lblContrasea)
						.addComponent(txtContraseña, GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
						.addComponent(btnLogin, Alignment.TRAILING))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(30)
					.addComponent(lblUsuario)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(txtUsuario, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(lblContrasea)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(txtContraseña, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(29)
					.addComponent(btnLogin)
					.addContainerGap(99, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
	
	public int getEmpleado()
	{
		int iEmpleado2 = iEmpleado;
		return iEmpleado2;
	}
	public String getNombreEmpleado()
	{
		String sNombre = sNombreEmpleado;
		return sNombre;
	}
}
